package com.xjgc.eip.twice.zxjeip2chgq.controller;


import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xjgc.common.constant.Consants;
import com.xjgc.common.entity.ReturnResult;
import com.xjgc.common.enums.ReturnStatus;
import com.xjgc.common.utils.BetValUtil;
import com.xjgc.eip.twice.zxjeip2chgq.entity.ZxjEip2cHgq;
import com.xjgc.eip.twice.zxjeip2chgq.service.ZxjEip2cHgqService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  互感器信息抓取接口
 * </p>
 *
 * @author zhanghongbin
 * @since 2019-11-23
 */
@Api("互感器信息抓取接口")
@RestController
@RequestMapping("/apis/zxjEip2cHgq")
public class ZxjEip2cHgqController {
        @Autowired
        ZxjEip2cHgqService zxjEip2cHgqService;

        /**
         * 互感器信息抓取接口
         */
        @ApiOperation("互感器信息抓取接口")
        @ApiImplicitParams({
                @ApiImplicitParam(paramType = "query", name = "amount", value = "查询条数",
                        required = true, dataType = "int")
        })
        @GetMapping(value = "/getData")
        @ResponseBody
        public ReturnResult getData(Integer amount,@RequestHeader(name = "supplierCode",required = false)String supplierCode) {
            ReturnResult returnResult = new ReturnResult();
            QueryWrapper<ZxjEip2cHgq> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("\"IOFLAG\"", Consants.Unread)
                    .last("limit " + (amount == null ? 100 : amount));
            if(StringUtils.isNotBlank(supplierCode)){
                queryWrapper.eq("\"SUPPLIER_CODE\"",supplierCode);
            }
            try {
                List<ZxjEip2cHgq> result = zxjEip2cHgqService.list(queryWrapper);

                //更新查询的数据为已读
                if (result != null && result.size() > 0) {
                    for (ZxjEip2cHgq zed : result) {
                        zed.setIoflag(Consants.read);
                        zed.setUpdateTime(new Date());
                    }
                    if (zxjEip2cHgqService.updateBatchById(result)) {
                        returnResult.setStatus(Integer.valueOf(ReturnStatus.OK.getCode()));
                        returnResult.setData(result);
                    } else {
                        returnResult.setStatus(Integer.valueOf(ReturnStatus.FAIL.getCode()));
                        returnResult.setMessage("请求异常");
                    }
                }
            } catch (Exception e) {
                returnResult.setStatus(Integer.valueOf(ReturnStatus.FAIL.getCode()));
                returnResult.setMessage("请求异常");
            }
            return returnResult;
        }
    @GetMapping(value = "/list")
    @DS("proxyDs")
    public Result<?> queryPageList(ZxjEip2cHgq zxjEip2cHgq,
                                   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                   HttpServletRequest req) throws ParseException {
        QueryWrapper<ZxjEip2cHgq> queryWrapper = new QueryWrapper<>();
        queryWrapper.setEntity(zxjEip2cHgq);

        //区间查询
        Map<String, String> map = BetValUtil.betValue(zxjEip2cHgq, req.getParameterMap());
        if(map.containsKey("inputVal_begin")){
            queryWrapper.ge("inputVal",map.get("inputVal_begin"));
        }
        if (map.containsKey("inputVal_end")){
            queryWrapper.le("inputVal",map.get("inputVal_end"));
        }
        //
        Page<ZxjEip2cHgq> page = new Page<ZxjEip2cHgq>(pageNo, pageSize);
        IPage<ZxjEip2cHgq> pageList = zxjEip2cHgqService.page(page, queryWrapper);
        return Result.ok(pageList);
    }
    }

